<?php
namespace App\Services\Admin;

use Illuminate\Support\Facades\DB;

class AdminService extends BaseService
{
    /**
     * 管理员
     */

    /**
     * 获取列表
     * @param array $pages【start起始页，limit每页记录数】
     * return array
     */
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbAdmin);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有管理员');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'name' => $v['name'],
                'lasttime' => $v['lasttime'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res, 'total' => $total);
    }

    /**
     * 修改
     */
    public function modify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbAdmin)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '没有记录');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbAdmin)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    //获取最近登录日志
    public function getLog($aid)
    {
        return (array) $this->dbConn($this->dbAdmLog)
            ->where('adminid', $aid)
            ->first();
    }

    //设置登录日志
    public function setLog($pas)
    {
        $pas['addtime'] = toDate();
        return $this->dbConn($this->dbAdmLog)->insertGetId($pas);
    }

    /**
     * 登录
     * @param string $name 管理员名称
     * @param string $passwd 密码
     * return array
     */
    public function doLogin($name, $passwd)
    {
        //有没有管理员
        $adminOne = (array) $this->dbConn($this->dbAdmin)
            ->where('name', $name)
            ->first();
        if (empty($adminOne)) {
            return $this->toback(-2, '没有管理员');
        }
        if ($adminOne['passwd'] != md5($passwd)) {
            return $this->toback(-2, '密码错误');
        }
        DB::beginTransaction();
        try {
            $clientIp = getClientIp();
            $lasttime = date('Y-m-d H:i:s');
            //更新管理员最后IP
            $adminData = [
                'lastip' => $clientIp,
                'lasttime' => $lasttime,
            ];
            $adminUp = $this->dbConn($this->dbAdmin)
                ->where('id', $adminOne['id'])
                ->update($adminData);
            if (!$adminUp) {
                return $this->toback(-1, '管理员错误');
            }
            //设置记录
            $logData = [
                'adminid' => $adminOne['id'],
                'adminName' => $adminOne['name'],
                'ip' => $clientIp,
            ];
            $res = $this->setLog($logData);
            if (!$res) {
                return $this->toback(-2, '日志错误');
            }
            DB::commit();
            return $this->toback(0, '', $adminOne);
        } catch (\Exception $e) {
            DB::rollBack();
            echo $e->getMessage();
            return $this->toback(-1, 'error');
        }
    }
}